var fs = require("fs");
var path = require("path");
var myUrl = require("url");
var queryString = require("querystring");
var formidable = require("formidable");
var session = require("express-session");
var md5 = require("blueimp-md5");
var myModule = require("./myModule.js");
var usersModule = require("./usersModule.js");

//展示首页静态数据
module.exports.getIndexPage = function (req, res) {
    // res.send("首页");
    myModule.getAllData((err, data) => {
        if (err) {
            res.json({ code: 0, msg: "页面加载失败" });
        }
        else {
            res.render(__dirname + "/views/index.html", { "heros": data, "current": req.session.currentUser }, (indexErr, indexData) => {
                if (indexErr) {
                    res.json({ code: 0, msg: "页面加载失败" });
                }
                else {
                    res.send(indexData);
                }
            })
        }
    })
}
//展示新增静态数据
module.exports.getAddPage = function (req, res) {
    res.render(__dirname + "/views/add.html");
}
//上传图片
module.exports.postupload = function (req, res) {
    var form = new formidable.IncomingForm();
    form.uploadDir = './upload';
    form.keepExtensions = true;
    form.parse(req, function (err, fields, files) {
        if (err) {
            res.json({ code: 0, msg: "上传图片失败" });
        }
        else {
            if (fields.last) {
                fs.unlink("./upload/" + fields.last, (err) => { })
            }
            var obj = {
                code: 1,
                msg: "上传成功",
                img: path.basename(files.myPic.path)
            }
            res.json(obj);
        }
    });

}
//实现新增操作
module.exports.sendAddReq = function (req, res) {
    var form = new formidable.IncomingForm();
    form.parse(req, (err, fields, files) => {
        if (err) {
            res.end("err");
        }
        else {
            var obj = fields;
            myModule.insertData(obj, (err, data) => {
                if (err) {
                    res.json({ code: 0, msg: "新增失败" });
                }
                else {
                    res.json({ code: 1, msg: "新增成功" });
                }
            });
        }
    })

}
//展示编辑静态数据
module.exports.getEditPage = function (req, res) {
    var id = myUrl.parse(req.url, true).query.id;
    myModule.queryDataById(id, (err, data) => {
        if (err) {
            res.json({ code: 0, msg: "加载编辑页面失败" });
        }
        else {
            res.render(__dirname + "/views/edit.html", data);
        }
    })
}
//实现编辑操作
module.exports.sendEditReq = function (req, res) {
    var str = "";
    req.on("data", (chunk) => {
        str += chunk;
    })
    req.on("end", () => {
        var obj = myUrl.parse("?" + str, true).query;
        myModule.updateDataById(obj, (err, data) => {
            if (err) {
                res.json({ code: 0, msg: "编辑失败" });
            }
            else {
                res.json({ code: 1, msg: "编辑成功" });
            }
        })
    })

}
//实现删除操作
module.exports.sendDelReq = function (req, res) {
    var id = myUrl.parse(req.url, true).query.id;
    myModule.delDataById(id, (err, data) => {
        if (err) {
            res.json({ code: 0, msg: "删除失败" });
        }
        else {
            res.json({ code: 1, msg: "删除成功" });
        }
    })
}
//展示登录静态数据
module.exports.getLoginPage = function (req, res) {
    res.render(__dirname + "/views/login.html");
}
//实现登录操作
module.exports.sendLoginReq = function (req, res) {
    var str = "";
    req.on("data", (chunk) => {
        str += chunk;
    })
    req.on("end", () => {
        var loginData = queryString.parse(str);
        usersModule.queryDataByUserName(loginData.userName, (err, data) => {
            if (err) {
                res.json({ code: 0, msg: "服务器异常，请重试" });
            }
            else {
                if (!data) {
                    res.json({ code: 0, msg: "用户名不存在" });
                }
                else {
                    if (data.password == md5(loginData.password)) {
                        req.session.isLogin = true;
                        req.session.currentUser = loginData;
                        res.writeHead(302, { Location: "/" });
                        res.end();
                    }
                    else {
                        res.send("<script>alert('密码错误...请重新输入');</script>");
                    }
                }
            }
        })
    })
}
//展示注册静态数据
module.exports.getRegisterPage = function (req, res) {
    res.render(__dirname + "/views/register.html");
}
//实现注册操作
module.exports.sendRegisterReq = function (req, res) {
    var str = "";
    req.on("data", (chunk) => {
        str += chunk;
    })
    req.on("end", () => {
        var formData = queryString.parse(str);
        formData.password = md5(formData.password);
        usersModule.insertNewUser(formData, (err, data) => {
            if (err) {
                res.json({ code: 0, msg: "注册失败" });

            }
            else {
                res.json({ code: 1, msg: "注册成功,快去登录吧" });
            }
        })
    })
}
